package com.lgt_enterprise_android.view.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.adapter.MyStaffAdapter;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.view.pages.InviteRecordActivity;
import com.lgt_enterprise_android.view.pages.b.InviteStaffActivity;
import com.lgt_enterprise_android.view.pages.b.staff.StaffDetailActivity;
import com.swipemenulistview.SwipeMenu;
import com.swipemenulistview.SwipeMenuCreator;
import com.swipemenulistview.SwipeMenuItem;
import com.swipemenulistview.SwipeMenuListView;
import com.util.AppUtil;
import com.widgets.CustomizeTitleView;

public class StaffFragment extends Fragment {

    private SwipeMenuListView mSwipeMenuListView;
    private MyStaffAdapter adapter;

    private JSONArray mJsonArray = new JSONArray();

    private int pageIdx = 1;


    public StaffFragment() {
        // Required empty public constructor
    }

    public static StaffFragment newInstance(String param1, String param2) {
        StaffFragment fragment = new StaffFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!isHidden()){
            getData();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            getData();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();

        getActivity().findViewById(R.id.txt_nostaff_goto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), InviteStaffActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initView() {

        ((CustomizeTitleView)getActivity().findViewById(R.id.customizeTitleView_second)).setMoreListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), InviteRecordActivity.class);
                startActivity(intent);
            }
        });

        mSwipeMenuListView = (SwipeMenuListView) getActivity().findViewById(R.id.swipeMenuListView);
        mSwipeMenuListView.onLoadComplete();

        mSwipeMenuListView.setOnRefreshListener(new SwipeMenuListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageIdx = 1;
                getData();
            }
        });


        mSwipeMenuListView.setOnLoadListener(new SwipeMenuListView.OnLoadListener() {
            @Override
            public void onLoad() {
                pageIdx++;
                getData();
                mSwipeMenuListView.onLoadComplete();
            }
        });

        mSwipeMenuListView.isShowIsload(false);
        mSwipeMenuListView.setMenuCreator(new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                if (menu.getViewType() == 0) {
                    SwipeMenuItem item = new SwipeMenuItem(getActivity());
                    item.setBackground(new ColorDrawable(Color.rgb(0xf4, 0x50, 0x2c)));
                    item.setWidth(110 * AppUtil.getdensityIntDpi(getActivity()));
                    item.setTitle("再次邀请");
                    item.setTitleSize(12);
                    item.setTitleColor(Color.WHITE);
                    menu.addMenuItem(item);
                } else {

                }
            }
        });

        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                JSONObject jo = ((JSONObject) adapter.getItem(position)).getJSONObject("workerInfo");
                JSONObject tmp = new JSONObject();
                tmp.put("name", jo.getString("empName"));
                tmp.put("phoneNumber", jo.getString("phoneNumber"));

                JSONArray jsonArray=new JSONArray();
                jsonArray.add(tmp);

                HttpManager.inviteStaff(getActivity(), jsonArray, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        Toast.makeText(getActivity(),"邀请成功",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });
            }
        });

        mSwipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                if (pos == 0) {
                    return;
                }
                Intent intent = new Intent(getActivity(), StaffDetailActivity.class);
                intent.putExtra("staff_id", adapter.getStaffIdByPos(pos - 1));
                startActivity(intent);
            }
        });


        adapter = new MyStaffAdapter(getActivity(), mJsonArray);
        mSwipeMenuListView.setAdapter(adapter);
    }

    private void getData() {
        HttpManager.getStaffList(getContext(), pageIdx, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                JSONArray jsonArray = ((JSONObject) resonseObj).getJSONArray("list");
                if (pageIdx == 1) {
                    if(jsonArray.size()==0){
                        getActivity().findViewById(R.id.lin_no_staff).setVisibility(View.VISIBLE);
                    }else{
                        getActivity().findViewById(R.id.lin_no_staff).setVisibility(View.GONE);
                        adapter.setData(jsonArray);
                        mSwipeMenuListView.onRefreshComplete();
                    }
                } else {
                    adapter.addData(jsonArray);
                    mSwipeMenuListView.onLoadComplete();
                }
                mSwipeMenuListView.setResultSize(jsonArray.size());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String mes) {

            }
        });

    }
}
